package connect;

import model.Info;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class InfoDao {
    /**
     * 根据SQL语句查询信息
     * @param sql
     * @param arr
     * @return 查到的信息的列表
     */
    public List<Info> selectInfo(String sql, Object arr[]){
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            // 1.连接数据库
            con = BaseDao.getConnection();
            // 2.预编译
            ps = con.prepareStatement(sql);
            if(arr != null){
                for (int i = 0; i < arr.length; i++) {
                    ps.setObject(i+1,arr[i]);
                }
            }
            // 3.执行sql
            rs = ps.executeQuery();
            // 4.保存查询出来的数据到list
            List<Info> list = new ArrayList<Info>();
            while(rs.next()){
                Info info = new Info();
                // 从数据库中获取值到实体类的setter方法中
                info.setId(rs.getInt("id"));
                info.setPrice(rs.getInt("price"));
                info.setStatus(rs.getInt("status"));
                info.setOrg(rs.getInt("org"));
                info.setName(rs.getString("username"));
                // 测试数据
                // 将对象保存到list中
                list.add(info);
            }
            return list;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            // 关闭资源，避免出现异常
            BaseDao.close(con,ps,rs);
        }
        return null;
    }

    /**
     * 添加一条记录到数据库
     * @param info
     * @return
     */
    public boolean addInfo(Info info){
        String sql = "insert into info values (?,?,?,?,?)";
        List<Object> list = new ArrayList<Object>();
        list.add(info.getId());
        list.add(info.getPrice());
        list.add(info.getStatus());
        list.add(info.getOrgNum());
        list.add(info.getName());
        boolean flag = BaseDao.addUpdateDelete(sql,list.toArray());
        if(flag){
            return true;
        }else {
            return false;
        }
    }


    /**
     * 更新指定组织和交易单号的信息的状态位，将其改为1
     * @param trade
     * @param org
     * @return
     */
    public boolean updateStatusByOrg(int trade,int org){
        String sql = "update info set status=? where (id-(id%10))/10=? and org=?";
        List<Object> list = new ArrayList<Object>();
        list.add(1);
        list.add(trade);
        list.add(org);
        boolean flag = BaseDao.addUpdateDelete(sql,list.toArray());
        if(flag){
            System.out.println("更新为1成功！");
            return true;
        }else {
            return false;
        }
    }

    /**
     * 更新指定交易单号的状态
     * @param trade
     * @param status
     * @return
     */
    public boolean updateStatusByTrade(int trade, int status){
        String sql = "update info set status=? where (id-(id%10))/10=? and status=?";
        List<Object> list = new ArrayList<Object>();
        list.add(status+1);
        list.add(trade);
        list.add(status);
        boolean flag = BaseDao.addUpdateDelete(sql,list.toArray());
        if(flag){
            System.out.println("更新状态（+1）成功！");
            return true;
        }else {
            return false;
        }
    }
    /**
     * 找到当前的交易单号
     * @return
     */
    public int getCurrent(){
        String sql="select * from info where id =(select id from info where id>=ALL(select id from info));";
        List<Info> list=selectInfo(sql,null);
        if (list.isEmpty()){
            return  0;
        }
        return list.get(0).getId()/10;
    }

    /**
     * 获取某个组织的所有交易单号
     * @param org 交易单号
     * @return 交易单号的链表
     */
    public List<Integer> getTradeByOrgAndStatus(int org, int status){
        List<Integer> re=new ArrayList<Integer>();
        int current = getCurrent();
        for(int i = 0;i<=current;i++){
            List<Info> temp= getInfoByTradeAndOrg(i,org,status);
            if (!temp.isEmpty()){
                re.add(i);
            }
        }
        return re;
    }

    public List<Integer> getTradeByNameAndStatus(String name,int status){
        if(name==null){
            return null;
        }
        List<Integer> re=new ArrayList<Integer>();
        int current = getCurrent();
        for(int i = 0;i<=current;i++){
            List<Info> temp= getInfoByTradeAndNameAndStatus(i,name,status);
            if (!temp.isEmpty()){
                re.add(i);
            }
        }
        return re;
    }
    /**
     * 获得某个状态的所有交易单号列表
     * @param status
     * @return
     */
    public List<Integer> getTradeByStatus(int status){
        List<Integer> re=new ArrayList<Integer>();
        int current = getCurrent();
        for(int i = 0;i<=current;i++){
            List<Info> temp= getInfoByTradeAndStatus(i,status);
            if (!temp.isEmpty()){
                re.add(i);
            }
        }
        return re;
    }
    /**
     * 根据交易号和组织号选择信息（一个模块内的信息）
     * @param trade
     * @param org
     * @return
     */
    public  List<Info> getInfoByTradeAndOrg(int trade, int org, int status){
        List<Object> paralist = new ArrayList<Object>();
        paralist.add(trade);
        paralist.add(org);
        paralist.add(status);
        String sql="select * from info where (id-(id%10))/10=? and org=? and status=?";
        List<Info> infoList= new ArrayList<Info>();
        infoList=selectInfo(sql,paralist.toArray());
        return infoList;
    }

    /**
     * 根据交易单号、和名字来找信息列表
     * @param trade
     * @param name
     * @return
     */
    public  List<Info> getInfoByTradeAndNameAndStatus(int trade,String name,int status){
        List<Object> paralist = new ArrayList<Object>();
        paralist.add(trade);
        paralist.add(status);
        paralist.add(name);

        String sql="select * from info where (id-(id%10))/10=? and status=? and username = ? ";
        List<Info> infoList= new ArrayList<Info>();
        infoList=selectInfo(sql,paralist.toArray());
        return infoList;

    }
    /**
     * 根据交易单号和找到信息
     * @param trade
     * @param status >=1
     * @return
     */
    public  List<Info> getInfoByTradeAndStatus(int trade, int status){
        List<Object> paralist = new ArrayList<Object>();
        paralist.add(trade);
        paralist.add(status);
        String sql="select * from info where (id-(id%10))/10=? and status=?";
        List<Info> infoList= new ArrayList<Info>();
        infoList=selectInfo(sql,paralist.toArray());
        return infoList;

    }

    public static void main(String[] args) {

    }
}
